home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / regexdemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-09  |  2.6 KB  |  91 lines

  1. {
  2. GPC demo program for the RegEx unit.
  3. Regular expression matching and replacement.
  4.  
  5. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  6.  
  7. Author: Frank Heckenbach <frank@pascal.gnu.de>
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, version 2.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22.  
  23. As a special exception, if you incorporate even large parts of the
  24. code of this demo program into another program with substantially
  25. different functionality, this does not cause the other program to
  26. be covered by the GNU General Public License. This exception does
  27. not however invalidate any other reasons why it might be covered
  28. by the GNU General Public License.
  29. }
  30.  
  31. program RegExDemo;
  32.  
  33. uses RegEx;
  34.  
  35. var
  36.   a : String(100);
  37.   r : RegExType;
  38.   i, j, p, l, o : Integer;
  39.   x, c, n, b, e : Boolean = False;
  40.  
  41. begin
  42.   Writeln ('Enter options: e(X)tended, (C)ase-insensitive, (N)ewlines,');
  43.   Write ('no (B)eginning/(E)nd of line: ');
  44.   Readln (a);
  45.   for i := 1 to Length (a) do
  46.     case UpCase (a [i]) of
  47.       'X' : x := True;
  48.       'C' : c := True;
  49.       'N' : n := True;
  50.       'B' : b := True;
  51.       'E' : e := True;
  52.       else Writeln ('Invalid option `', a [i], '''')
  53.     end;
  54.   Write ('Enter regular expression: ');
  55.   Readln (a);
  56.   NewRegEx (r, a, x, c, n);
  57.   if r.Error <> nil then
  58.     begin
  59.       Writeln (r.Error^);
  60.       Halt
  61.     end;
  62.   Writeln ('The regex contains ', r.SubExpressions, ' subexpressions.');
  63.   repeat
  64.     Writeln ('Enter a string to match (empty string when finished):');
  65.     Readln (a);
  66.     if a = '' then Break;
  67.     Write ('Search from position: ');
  68.     Readln (o);
  69.     Writeln (a);
  70.     if not MatchRegExFrom (r, a, b, e, o) then
  71.       Writeln ('No match.')
  72.     else
  73.       for i := 0 to r.SubExpressions do
  74.         begin
  75.           GetMatchRegEx (r, i, p, l);
  76.           if p = 0 then
  77.             Writeln ('-')
  78.           else
  79.             begin
  80.               Write ('' : p - 1);
  81.               if l = 0 then
  82.                 Write ('0')
  83.               else
  84.                 for j := 1 to l do Write ('^');
  85.               Writeln
  86.             end
  87.         end
  88.   until False;
  89.   DisposeRegEx (r)
  90. end.
  91.